/**
 * 
 */
package client.gui.panels;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import controllers.ControllerIF;
import controllers.ReviewDecisionController;
import entities.ReviewDecisionStage;

/**
 * @author Vodi
 * Review decision panel including the committee board's decision
 */
public class ReviewDecisionPanel extends JPanel implements PanelIF, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the review decision panel controller
	 */
	private ReviewDecisionController reviewDecisionController;

	/**
	 * Structure holding all the data that the committee inserted into the ticket
	 */
	private ReviewDecisionStage reviewDecisionStage;

	private JButton submitButton;
	private JComboBox<String> comboBox;
	
	
	public ReviewDecisionPanel(ReviewDecisionStage reviewDecisionStage) {
		this.reviewDecisionStage = reviewDecisionStage;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(Box.createVerticalStrut(20));
		
		JPanel datePanel = new JPanel();
		add(datePanel);
		datePanel.setLayout(new BoxLayout(datePanel, BoxLayout.X_AXIS));
		
		JLabel datePromptLabel = new JLabel("Review decision due date: ");
		datePanel.add(datePromptLabel);
		
		JLabel dateLabel = new JLabel(reviewDecisionStage.getDurationDate());
		datePanel.add(dateLabel);
		
		add(Box.createVerticalStrut(20));
		
		JPanel decisionPanel = new JPanel();
		add(decisionPanel);
		decisionPanel.setLayout(new FlowLayout());
		
		JLabel inputPromptLabel = new JLabel("Committee's decision about this ticket is:");
		decisionPanel.add(inputPromptLabel);
		
		decisionPanel.add(Box.createHorizontalStrut(20));
		
		comboBox = new JComboBox<String>();
		comboBox.setToolTipText("Select one option from this list");
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"", "Approves execution", "Rejects execution", "Request for further details"}));
		comboBox.addActionListener(this);
		decisionPanel.add(comboBox);
		
		add(Box.createVerticalStrut(20));
		
		JPanel buttonPanel = new JPanel();
		add(buttonPanel);
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		
		buttonPanel.add(Box.createHorizontalGlue());
		
		submitButton = new JButton("Submit");
		submitButton.setEnabled(false);
		submitButton.addActionListener(this);
		buttonPanel.add(submitButton);
		
		buttonPanel.add(Box.createHorizontalStrut(20));
	}

	/* (non-Javadoc)
	 * @see client.gui.panels.PanelIF#setController(controllers.ControllerIF)
	 */
	@Override
	public void setController(ControllerIF cif) {
		reviewDecisionController = (ReviewDecisionController) cif;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(comboBox)) {
			if(comboBox.getSelectedItem().toString().equals("")) {
				submitButton.setEnabled(false);
			}
			else 
				submitButton.setEnabled(true);
		}
		else if(e.getSource().equals(submitButton)) {
			reviewDecisionController.action();
		}
	}

	/**
	 * Get the committee decision 
	 * @return
	 */
	public String getCommitteeDecision() {
		if(comboBox.getSelectedItem().equals("Approves execution")) {
			return "\"APPROVED\"";
		}
		else if(comboBox.getSelectedItem().equals("Rejects execution")) {
			return "\"DECLINED\"";
		}
		else if(comboBox.getSelectedItem().equals("Request for further details")) {
			return "\"RQST_DATA\"";
		}
		else
			return "";
	}

}
